package com.smartlink.app.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smartlink.app.R;
import com.smartlink.app.activity.BluetoothActivity;
import com.smartlink.app.activity.LoginActivity;
import com.smartlink.app.activity.SetIpActivity;
import com.smartlink.app.util.NetWorkUtil;


public class UserFragment extends Fragment implements View.OnClickListener {


    // Content View Elements
    private TextView tv_user_name;
    private Button btn_user_save;
    private Button btn_user_search;
    private Button btn_user_logout;

    private SharedPreferences sharedPreferences;

    public UserFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user, container, false);
        sharedPreferences = getContext().getSharedPreferences("main", Context.MODE_PRIVATE);

        bindViews(rootView);
        return rootView;
    }


    // End Of Content View Elements

    private void bindViews(View rootView) {
        tv_user_name = (TextView) rootView.findViewById(R.id.tv_user_name);
        btn_user_save = (Button) rootView.findViewById(R.id.btn_user_save);
        btn_user_search = (Button) rootView.findViewById(R.id.btn_user_search);
        btn_user_logout = (Button) rootView.findViewById(R.id.btn_user_logout);
        btn_user_save.setOnClickListener(this);
        btn_user_logout.setOnClickListener(this);
        btn_user_search.setOnClickListener(this);
        btn_user_save.setEnabled(true);
        String name = sharedPreferences.getString("name", "");
        tv_user_name.setText(name);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_user_save:
                startSetIpActivity();
                break;
            case R.id.btn_user_search:
                searchClassRoom();
                break;
            case R.id.btn_user_logout:
                logout();
                break;
        }

    }

    private void startSetIpActivity() {
        Intent intent = new Intent(getContext(), SetIpActivity.class);
        startActivity(intent);
    }


    private void searchClassRoom() {
        Intent intent = new Intent(getContext(), BluetoothActivity.class);
        getContext().startActivity(intent);
    }

    private void logout() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove("number");
        editor.remove("name");
        editor.remove("password");
        editor.remove("userType");
        editor.apply();
        Intent intent = new Intent(getContext(), LoginActivity.class);
        intent.putExtra("error", "请重新登陆");
        startActivity(intent);
        getActivity().finish();
    }
}
